package com.dianrui.fmi.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dianrui.fmi.Constant;
import com.dianrui.fmi.R;

import com.dianrui.fmi.util.SPUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.ToastUtil;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 多语言
 */
public class ChangeLanguageActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.english)
    ImageView english;
    @BindView(R.id.burmese)
    ImageView burmese;

    String language_flag = "";
    SPUtils spUtils;


    @Override
    public int getContentViewId() {
        return R.layout.language;
    }

    @Override
    public void initView() {
        topRight.setText(getString(R.string.save));
        topRight.setVisibility(View.VISIBLE);
        title.setText(getString(R.string.languages));
        initConfig();
    }

    private void initConfig() {
        if (spUtils == null) {
            spUtils = new SPUtils(this, Constant.cfgFileName, Context.MODE_PRIVATE);
        }
        if (!StringUtils.isEmpty(spUtils.getString("language_flags"))) {
            switch (spUtils.getString("language_flags")) {
                case "one":
                    english.setVisibility(View.VISIBLE);
                    burmese.setVisibility(View.GONE);
                    break;
                case "two":
                    english.setVisibility(View.GONE);
                    burmese.setVisibility(View.VISIBLE);
                    break;

                    default:
                        break;
            }

        }
    }


    @OnClick({R.id.back, R.id.click_layout1, R.id.click_layout2, R.id.top_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.click_layout1:
                english.setVisibility(View.VISIBLE);
                burmese.setVisibility(View.GONE);
                language_flag = "1";
                spUtils.put("language_flags", "one");
                spUtils.put("language", "english");
                break;
            case R.id.click_layout2:
                english.setVisibility(View.GONE);
                burmese.setVisibility(View.VISIBLE);
                language_flag = "2";
                spUtils.put("language_flags", "two");
                spUtils.put("language", "burma");
                break;

            case R.id.top_right:
                switch (language_flag){
                    case "1":
                        Resources resources = getResources();
                        Configuration configuration = resources.getConfiguration();
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                            configuration.setLocale(Locale.ENGLISH);//英语
                        }
                        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
                        resources.updateConfiguration(configuration, displayMetrics);
                        break;

                    case "2":
                        Resources rmmResources = getResources();
                        Configuration rmmConfiguration = rmmResources.getConfiguration();
                        Locale myLocale = new Locale("my", "rMM");
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                            rmmConfiguration.setLocale(myLocale);//缅甸语
                        }else {
                            rmmConfiguration.locale = myLocale;
                        }
                        DisplayMetrics rmmDisplayMetrics = rmmResources.getDisplayMetrics();
                        rmmResources.updateConfiguration(rmmConfiguration, rmmDisplayMetrics);
                        break;

                        default:
                            break;
                }
                ToastUtil.showToast(getString(R.string.setting_success));
                Intent intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
                break;

                default:
                    break;
        }
    }
}
